<?php
$this->LoadHelper('ApiHelper');
$ApiHelper = new ApiHelper();

$check = $ApiHelper->tokenCheck($this->Param);
if(!$check['success']) ErrorMsg::ApiMsg($check['msg'], $check['code']);


$this->LoadHelper('AttachHelper');
$tb = empty($this->Param['tb']) ? 'order_inquirycert' : $this->Param['tb'];
$AttachHelper = new AttachHelper($this->Param['uid'], $tb);
$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();
$member_info = $MemberHelper->getMember($this->Param['uid']);
$this->Assign('member_info', $member_info);
$picture_array = array('order_inquirycert','order_family','order_sign','getcar_img','estimate_car');
$cert_keyarr = array('sfzzm','sfzfm','sqs','qzz');
switch ($this->Param['option']){
    case 'upimage':
        $type = empty($this->Param['type']) ? 'default' : $this->Param['type'];
        $filebox = empty($this->Param['filebox']) ? 'file' : $this->Param['filebox'];
        $oid = !empty($this->Param['oid']) && is_numeric($this->Param['oid']) && $this->Param['oid']>0 ? $this->Param['oid'] : 0;
        if($oid==0 && (empty($this->Param['temp_id']) || !is_numeric($this->Param['temp_id']) || strlen($this->Param['temp_id'])!=9)){
            ErrorMsg::ApiMsg('处理失败：'.'请尝试更新至最新版本解决此问题！');
        }else {
            $file = $_FILES[$filebox];
            $do = $AttachHelper->upImage($file);
            if ($do['path']) {
                $this->LoadResurces('aliyun/class.idcard');
                $idcardOcr = new Idcard();
                if ($type == 'face') {
                    $card_info = $idcardOcr->getCardInfo(trim($do['path'], '/'), 'face');
                } elseif ($type == 'back') {
                    $card_info = $idcardOcr->getCardInfo(trim($do['path'], '/'), 'back');
                }
                $do['cardinfo'] = empty($card_info['data']) ? '' : $card_info['data'];
                if(!in_array($tb,$picture_array)){
                    //文字识别OCR
                    $this->LoadResurces('baidu/AipOcr');
                    $client = new AipOcr('10926083', '2NWzy0KW30qBjy1eMvNv7IFF', 'eAei4XVOmYwbkAIFkHfopMqgpKIZy9F6');
                    $text = $client->basicAccurate(file_get_contents(trim($do['path'],'/')));
                    //$do['text'] = $text;
                    //插入附件表
                    $str = json_encode($text);
                    $str = preg_replace_callback("#\\\u([0-9a-f]{4})#i",function($matchs){
                        return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
                    },$str);
                }
                //$ApiHelper->logSave('upimage', $this->Param);
                //todo:yangshile,注意观察征信的标志 Param['key']是否正确
                $tb = in_array($this->Param['key'],$cert_keyarr)?'order_inquirycert':$tb;
                $attach = array(
                    'ot_oid' => $oid,
                    'temp_id' => !empty($this->Param['temp_id']) ? $this->Param['temp_id'] : 0,
                    'type' => 'jpeg',
                    'class' => $tb,
                    'url' => $do['ai_path'],
                    'img_text' => $str,
                    'is_ftp' => 0,
                    'user_name' => $member_info['u_realname'],
                    'image_name' => empty($this->Param['ofilename']) ? 'file' : $this->Param['ofilename']
                );
                $at_id = $AttachHelper->attachSave($attach);
                ErrorMsg::ApiMsg('处理成功', 0, $do);
            } else {
                ErrorMsg::ApiMsg('处理失败：' . $do['msg']);
            }
        }
        break;
    case 'video':
        set_time_limit(0);
        $filebox = empty($this->Param['filebox']) ? 'file' : $this->Param['filebox'];
        $file = $_FILES[$filebox];
        //漏洞
        $ext = array('mp4', 'wav', 'wma', 'wmv', 'flv', 'avi', 'mpg', 'asf', 'rm', 'rmvb');
        $temp_arr = explode(".", $file['name']);
    	$file_ext = array_pop($temp_arr);
    	$file_ext = trim($file_ext);
    	$file_ext = strtolower($file_ext);
        if(!in_array($file_ext, $ext)){
            ErrorMsg::ApiMsg('请上传视频文件');
        }
        $file['name'] = $this->Param['name']?$this->Param['name'].".".$file_ext:$file['name'];
        $do = $AttachHelper->upFile($file);
        if($this->Param['name']){
            $do['name'] = $this->Param['name'];
        }
        if($do['path']){
            ErrorMsg::ApiMsg('上传成功', 0, $do);
        }else{
            ErrorMsg::ApiMsg('上传失败');
        }
        break;
    default :
}